package com.qing.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qing.eduservice.entity.pojo.EduChapter;
import com.qing.eduservice.entity.pojo.EduVideo;
import com.qing.eduservice.entity.vo.ChapterVo;
import com.qing.eduservice.entity.vo.VideoVo;
import com.qing.eduservice.mapper.EduChapterMapper;
import com.qing.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qing.eduservice.service.EduVideoService;
import com.qing.servicebase.exception.JsonException;
import com.qing.servicebase.utils.ResponseResult;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author Yjx
 * @since 2022-09-08
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    private final EduVideoService eduVideoService;

    public EduChapterServiceImpl(EduVideoService eduVideoService) {
        this.eduVideoService = eduVideoService;
    }

    @Override
    public ResponseResult nestedList(String courseId) {
        //最终要的到的数据列表
        ArrayList<ChapterVo> chapterVoArrayList = new ArrayList<>();
        //获取章节信息
        QueryWrapper<EduChapter> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("course_id", courseId);
        queryWrapper1.orderByAsc("sort", "id");
        List<EduChapter> chapters = baseMapper.selectList(queryWrapper1);
        //获取课时信息
        QueryWrapper<EduVideo> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("course_id", courseId);
        queryWrapper2.orderByAsc("sort", "id");
        List<EduVideo> videos = eduVideoService.list(queryWrapper2);
        //填充章节vo数据
        int count1 = chapters.size();
        for (int i = 0; i < count1; i++) {
            EduChapter chapter = chapters.get(i);
            //创建章节vo对象
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(chapter, chapterVo);
            chapterVoArrayList.add(chapterVo);
            //填充课时vo数据
            ArrayList<VideoVo> videoVoArrayList = new ArrayList<>();
            int count2 = videos.size();
            for (int j = 0; j < count2; j++) {
                EduVideo video = videos.get(j);
                if (chapter.getId().equals(video.getChapterId())) {
                    //创建课时vo对象
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(video, videoVo);
                    videoVoArrayList.add(videoVo);
                }
            }
            chapterVo.setChildren(videoVoArrayList);
        }
        return ResponseResult.ofSuccess(chapterVoArrayList);
    }

    @Override
    public ResponseResult removeChapterById(String id) {
        //根据id查询是否存在视频，如果有则提示用户尚有子节点
        if (eduVideoService.getCountByChapterId(id).getCode() == 200) {
            throw new JsonException(201, "该分章节下存在视频课程，请先删除视频课程");
        }
        Integer result = baseMapper.deleteById(id);
        return ResponseResult.ofSuccess(null != result && result > 0);
    }

    @Override
    public ResponseResult addChapter(EduChapter eduChapter) {
        boolean save = this.save(eduChapter);
        return ResponseResult.ofSuccess(null);
    }

    @Override
    public ResponseResult searchById(String id) {
        EduChapter eduChapter = this.getById(id);
        return ResponseResult.ofSuccess(eduChapter);
    }

    @Override
    public ResponseResult updateById(String id, EduChapter chapter) {
        chapter.setId(id);
        this.updateById(chapter);
        return ResponseResult.ofSuccess(null);
    }

    @Override
    public ResponseResult removeById(String id) {
        ResponseResult responseResult = this.removeChapterById(id);
        return responseResult;
    }

    @Override
    public ResponseResult removeByCourseId(String courseId) {
        QueryWrapper<EduChapter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id", courseId);
        Integer count = baseMapper.delete(queryWrapper);
        return ResponseResult.ofSuccess(null != count && count > 0);
    }

    @Override
    public ResponseResult nestedList() {
        QueryWrapper<EduChapter> queryWrapper = new QueryWrapper<>();
        return null;
    }
}
